#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

static bool try_serve(const char *command)
{
  char tokens[3][16];
  int itoken;

  for(itoken = 0;; ++itoken) {  // Tokenization.
    while(*command && isspace(*command)) ++command;  // Skip white spaces.
    if(!*command) break;  // EOL
    if(itoken == 3) { itoken = -1; break; }  // Too many tokens.
    const char *word = command;  // Start of the token.
    while(*command && !isspace(*command)) ++command;  // Absorb non-white characters.
    size_t length = command - word;  // Length of the token.

    assert(length < sizeof tokens[itoken]);  // Check the length.
    strncpy(tokens[itoken], word, length);  // Store the token.
    tokens[itoken][length] = 0;
  }

  // Expect exact 3 tokens.
  if(itoken != 3) return false;

  // The first token must be "order".
  if(strcmp(tokens[0], "order")) return false;

  // The second token specifies the name of the product.
  const char *name = tokens[1];

  // The third token must represent a signed decimal integer.
  long quantity;
  char *end;
  quantity = strtol(tokens[2], &end, 10);
  if(*end) return false;

  // Everything validated, it's time to serve the order.
  if(quantity == 1) {
    printf("Orders a %s.\n", name);
  } else {
    printf("Orders %ld %ss.\n", quantity, name);
  }
  return true;
}

static void serve(const char *command)
{
  if(try_serve(command)) return;
  printf("The bar bursts into flames, killing everyone.\n");
  exit(1);
}

int main(void)
{
  printf("A QA engineer walks into a bar.\n");
  serve("order beer 1");
  serve("order beer 0");
  serve("order beer 99999999999");
  serve("order lizard 1");
  serve("order beer -1");
  serve("order ueicbksjdhd 1");

  printf("First real customer walks in and asks where the bathroom is.\n");
  serve("locate bathroom");
  return 0;
}
